package com.tencent.examples.core.temp;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tencent.bk.utils.blueking.properties.ApiUrlProperties;
import com.tencent.bk.utils.blueking.tools.HttpsClient;
import com.tencent.bk.utils.blueking.tools.ResultUtil.ResultDto;
import com.tencent.bk.utils.blueking.tools.ResultUtil.ResultUtil;
import com.tencent.bk.utils.blueking.tools.StringConverters;
import com.tencent.examples.blueking.extend.QueryParam;
import com.tencent.examples.blueking.mongo.model.ObjAttDes;
import com.tencent.examples.blueking.mongo.model.ObjClassification;
import com.tencent.examples.blueking.mongo.model.ObjDes;
import com.tencent.examples.blueking.mongo.model.ObjectBase;
import com.tencent.examples.blueking.mongo.service.*;
import com.tencent.examples.blueking.mysql.model.BkSaveNavigationBar;
import com.tencent.examples.blueking.mysql.service.BkSaveNavigationBarService;
import com.tencent.examples.blueking.mysql.service.MessageService;
import com.tencent.examples.core.processor.common.CommParams;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/web/admin/searchObjects")
public class searchObjectAction {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    CommParams commParams;
    @Autowired
    ApiUrlProperties apiUrlProperties;
    @Autowired
    ObjClassificationService objClassificationService;
    @Autowired
    ObjDesService objDesService;
    @Autowired
    ObjectBaseService objectBaseService;
    @Autowired
    ObjAttDesService objAttDesService;
    @Autowired
    InstAsstService instAsstService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private BkSaveNavigationBarService bkSaveNavigationBarService;


    public static Cookie getCookieByName(HttpServletRequest request, String name) {
        Map<String, Cookie> cookieMap = ReadCookieMap(request);
        if (cookieMap.containsKey(name)) {
            Cookie cookie = (Cookie) cookieMap.get(name);
            return cookie;
        } else {
            return null;
        }
    }

    private static Map<String, Cookie> ReadCookieMap(HttpServletRequest request) {
        Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                cookieMap.put(cookie.getName(), cookie);
            }
        }
        return cookieMap;
    }

    @RequestMapping("/index")
    @ResponseBody
    public JSONArray index() {
        messageService.findByMx();
        return new JSONArray();
    }


    /**
     * 根据bk_token获取用户名
     */
    @RequestMapping("/getUserNameByBkToken")
    @ResponseBody
    public JSONObject getUserNameByBkToken(HttpServletRequest request) {
        JSONObject result = new JSONObject();
        JSONObject jsonObject = commParams.initJsonOtherMethod(request);
        JSONObject resultObject = HttpsClient.httpRequest(apiUrlProperties.getGet_user(), "POST", jsonObject.toString());
        if (resultObject.getBoolean("result")) {
            JSONObject dataObject = resultObject.getJSONObject("data");
            result.put("bk_username", dataObject.getString("bk_username"));
        }

        return result;
    }


    /**
     * 显示列数据
     */
    @RequestMapping("/getDisplayColumn")
    @ResponseBody
    public ResultDto getDisplayColumn(HttpServletRequest request, String bk_obj_id) {
        String bk_userName = bkSaveNavigationBarService.getUserName(request);
        QueryParam queryParam = new QueryParam();
        if (StringUtils.isBlank(bk_obj_id)) {
            return ResultUtil.error("获取模块信息失败，请重试！");
        }
        if (StringUtils.isBlank(bk_userName)) {
            return ResultUtil.error("获取人员信息失败，请重试！");
        }
        queryParam.getSqlMap().put("bk_userName", bk_userName);
        queryParam.getSqlMap().put("bk_obj_id", bk_obj_id);
        List<BkSaveNavigationBar> bkSaveNavigationBarList = bkSaveNavigationBarService.findList(queryParam);
        if (CollectionUtils.isEmpty(bkSaveNavigationBarList)) {
            return ResultUtil.success("", null);
        }
        BkSaveNavigationBar bkSaveNavigationBar = bkSaveNavigationBarList.get(0);
        return ResultUtil.success("", bkSaveNavigationBar.getDisplayColumn());
    }

    /**
     * 保存显示列数据
     */
    @RequestMapping("/saveDisplayColumn")
    @ResponseBody
    public ResultDto saveDisplayColumn(HttpServletRequest request, String bk_obj_id, String displayColumn) {
        String bk_userName = bkSaveNavigationBarService.getUserName(request);
        QueryParam queryParam = new QueryParam();
        if (StringUtils.isBlank(bk_obj_id)) {
            return ResultUtil.error("获取模块信息失败，请重试！");
        }
        if (StringUtils.isBlank(bk_userName)) {
            return ResultUtil.error("获取人员信息失败，请重试！");
        }
        queryParam.getSqlMap().put("bk_userName", bk_userName);
        queryParam.getSqlMap().put("bk_obj_id", bk_obj_id);
        List<BkSaveNavigationBar> bkSaveNavigationBarList = bkSaveNavigationBarService.findList(queryParam);
        if (CollectionUtils.isEmpty(bkSaveNavigationBarList)) {
            BkSaveNavigationBar bkSaveNavigationBarAdd = new BkSaveNavigationBar();
            bkSaveNavigationBarAdd.setBk_obj_id(bk_obj_id);
            bkSaveNavigationBarAdd.setBk_userName(bk_userName);
            bkSaveNavigationBarAdd.setDisplayColumn(displayColumn);
            bkSaveNavigationBarService.add(bkSaveNavigationBarAdd);
            return ResultUtil.success();
        }
        BkSaveNavigationBar bkSaveNavigationBarUpdate = bkSaveNavigationBarList.get(0);
        bkSaveNavigationBarUpdate.setDisplayColumn(displayColumn);
        return ResultUtil.success();
    }


    /**
     * 左侧导航栏(1级，2级)
     *
     * @return
     */
    @RequestMapping("/getObjectList")
    @ResponseBody
    public JSONObject getObjectList(HttpServletRequest request, String indexType) {
//        String bkToken=request.getHeader("Authorization");
        JSONObject jsonObject = commParams.initJsonOtherMethod(request);
//        jsonObject.put("bk_token",bkToken);
        JSONObject result = new JSONObject();
        JSONArray jsonArrayRes = new JSONArray();

        //查询所有的一级分类
        List<ObjClassification> objClassificationList = objClassificationService.findFirstMenu();
        if (CollectionUtils.isNotEmpty(objClassificationList)) {
            for (int i = 0; i < objClassificationList.size(); i++) {
                ObjClassification objClassification = objClassificationList.get(i);
                if ("0".equals(indexType)) {
                    if ("星巴克门店".equals(objClassification.getBk_classification_name())) {
                        JSONArray search_objectsArray = new JSONArray();
                        JSONObject meunJSON = new JSONObject();
                        meunJSON.put("bk_classification_id", objClassification.getBk_classification_id());
                        meunJSON.put("bk_classification_name", objClassification.getBk_classification_name());
                        List<ObjDes> objDesList = objDesService.findSecondMenu(objClassification.getBk_classification_id());
                        if (CollectionUtils.isNotEmpty(objDesList)) {
                            for (int j = 0; j < objDesList.size(); j++) {
                                ObjDes objDes = objDesList.get(j);
                                JSONObject jsonObjectNew = new JSONObject();
                                jsonObjectNew.put("bk_obj_id", objDes.getBk_obj_id());
                                jsonObjectNew.put("bk_obj_name", objDes.getBk_obj_name());
                                jsonObjectNew.put("bk_supplier_account", objDes.getBk_supplier_account());
                                search_objectsArray.add(jsonObjectNew);
                                meunJSON.put("search_objectsArray", search_objectsArray);
                            }
                        }
                        jsonArrayRes.add(meunJSON);
                    }
                } else {
                    if (!"星巴克门店".equals(objClassification.getBk_classification_name())) {
                        JSONArray search_objectsArray = new JSONArray();
                        JSONObject meunJSON = new JSONObject();
                        meunJSON.put("bk_classification_id", objClassification.getBk_classification_id());
                        meunJSON.put("bk_classification_name", objClassification.getBk_classification_name());
                        List<ObjDes> objDesList = objDesService.findSecondMenu(objClassification.getBk_classification_id());
                        if (CollectionUtils.isNotEmpty(objDesList)) {
                            for (int j = 0; j < objDesList.size(); j++) {
                                ObjDes objDes = objDesList.get(j);
                                JSONObject jsonObjectNew = new JSONObject();
                                jsonObjectNew.put("bk_obj_id", objDes.getBk_obj_id());
                                jsonObjectNew.put("bk_obj_name", objDes.getBk_obj_name());
                                jsonObjectNew.put("bk_supplier_account", objDes.getBk_supplier_account());
                                search_objectsArray.add(jsonObjectNew);
                                meunJSON.put("search_objectsArray", search_objectsArray);
                            }
                        }
                        jsonArrayRes.add(meunJSON);
                    }

                }

            }

        }
        result.put("data", jsonArrayRes);
        return result;
    }

    /**
     * 删除-new
     *
     * @param bk_obj_id
     * @param bk_supplier_account
     * @param inst_ids
     * @param request
     * @return
     */
    @RequestMapping("/deleteTableRows")
    @ResponseBody
    public JSONObject deleteTableRows(String bk_obj_id, String bk_supplier_account, String inst_ids, HttpServletRequest request) {
        JSONArray jsonArray = JSONArray.parseArray(inst_ids);
        if (jsonArray.size() <= 0) {
            JSONObject result = new JSONObject();
            result.put("data", "删除失败，请选择需要删除的选项！");
            result.put("result", false);
            result.put("code", 1);
            result.put("message", "删除失败，请选择需要删除的选项！");
            return result;
        }

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = commParams.initJsonOtherMethod(request);
            jsonObject.put("bk_supplier_account", bk_supplier_account);
            jsonObject.put("bk_obj_id", bk_obj_id);
            JSONObject deleteJson = jsonArray.getJSONObject(i);
            if ("host".equals(bk_obj_id)) {
                jsonObject.put("bk_host_id", deleteJson.getString("instId"));
                JSONObject delete_host_URLJSON = HttpsClient.httpRequest(apiUrlProperties.getDelete_host_URL(), "POST", jsonObject.toString());
                if (!delete_host_URLJSON.getBoolean("result")) {
                    return delete_host_URLJSON;
                }
            } else {
                JSONObject delete = new JSONObject();
                JSONArray deleteArrayInst = new JSONArray();
                deleteArrayInst.add(StringConverters.ToInteger(deleteJson.getString("instId")));
                delete.put("inst_ids", deleteArrayInst);
                jsonObject.put("delete", delete);
                JSONObject delete_inst_URLJSON = HttpsClient.httpRequest(apiUrlProperties.getBatch_delete_inst_URL(), "POST", jsonObject.toString());
                if (!delete_inst_URLJSON.getBoolean("result")) {
                    return delete_inst_URLJSON;
                }
            }
        }
        JSONObject result = new JSONObject();
        result.put("data", "删除成功");
        result.put("result", true);
        result.put("code", 0);
        result.put("message", "删除成功");
        return result;
    }

    /**
     * 列表数据列名新--new
     *
     * @return
     */
    @RequestMapping("/getTableColumns")
    @ResponseBody
    public String getTableColumns(String bk_obj_id, String bk_supplier_account, HttpServletRequest request) {
        if (StringUtils.isEmpty(bk_obj_id)) {
            return JSONObject.toJSON(ResultUtil.error("bk_obj_id不能为空")).toString();
        }
        return objAttDesService.getTableList(bk_obj_id);
    }


    /**
     * 列表数据列数据--new
     *
     * @return
     */
    @RequestMapping("/getTableData")
    @ResponseBody
    public ResultDto getTableData(String bk_obj_id, String bk_supplier_account, HttpServletRequest request, String page, String size) {
        if (StringUtils.isEmpty(bk_obj_id)) {
            return ResultUtil.error("bk_obj_id不能为空");
        }
        Integer pageIndex = StringConverters.ToInteger(page);
        Integer sizeIndex = StringConverters.ToInteger(size);
        return objAttDesService.getTableListDataNew(bk_obj_id, bk_supplier_account, pageIndex, sizeIndex);
    }


    /**
     * 列表数据列名新--old
     *
     * @return
     */
    @RequestMapping("/getTableList")
    @ResponseBody
    public JSONObject getTableList1(String bk_obj_id, String bk_supplier_account, HttpServletRequest request) {
        if ("sbux_store_info".equals(bk_obj_id)) {
            List<ObjAttDes> objAttDesList = objAttDesService.getMdzInfo(bk_obj_id);
            JSONObject result = new JSONObject();
            if (CollectionUtils.isNotEmpty(objAttDesList)) {
                for (ObjAttDes objAttDes : objAttDesList) {
                    result.put(objAttDes.getBk_property_id(), objAttDes.getBk_property_name());
                }
            }
            return result;
        } else {
            return this.start(bk_obj_id, request);
        }
    }

    /**
     * 列表数据列数据
     *
     * @return
     */
    @RequestMapping("/getTableListData")
    @ResponseBody
    public JSONArray getTableListData1(String bk_obj_id, String bk_supplier_account, HttpServletRequest request) {
        if ("sbux_store_info".equals(bk_obj_id)) {
            List<ObjectBase> objectBaseList = objectBaseService.getQyInfo(bk_obj_id);
            JSONArray jsonArray = new JSONArray();
            int i = 0;
            if (CollectionUtils.isNotEmpty(objectBaseList)) {
                for (ObjectBase objectBase : objectBaseList) {
                    JSONObject jsonObject = (JSONObject) JSONObject.toJSON(objectBase);
                    jsonObject.put("useeIndex", i);
                    i++;
                    jsonArray.add(jsonObject);
                }
            }
            return jsonArray;
        } else {
//        JSONObject request = commParams.initJson();
//            String bkToken = request.getHeader("Authorization");
            JSONObject jsonObject = commParams.initJsonOtherMethod(request);
//        jsonObject.put("bk_token",bkToken);
            try {
                String zj = "";
                //模型的bk_obj_id 获取实例的信息
                jsonObject.put("bk_obj_id", bk_obj_id);
                jsonObject.put("bk_supplier_account", bk_supplier_account);
                JSONObject page = new JSONObject();
                page.put("start", 0);  //page_name:前端回传参数
                page.put("limit", 100);
                jsonObject.put("page", page);
                JSONObject search_inst_url_JSON = HttpsClient.httpRequest(apiUrlProperties.getSearch_inst_url(), "POST", jsonObject.toString());
                JSONArray sl_JSONArray = search_inst_url_JSON.getJSONObject("data").getJSONArray("info");

                JSONObject search_object_attribute_URLJSON = HttpsClient.httpRequest(apiUrlProperties.getSearch_object_attribute_URL(), "POST", jsonObject.toString());
                //判断模型的bk_obj_id 获取模型的所有属性 中的bk_asst_obj_id是否为空，若不为空，就包获取到相关的主机信息
                JSONArray jsonArray = search_object_attribute_URLJSON.getJSONArray("data");
                for (int a = 0; a < jsonArray.size(); a++) {
                    JSONObject x = jsonArray.getJSONObject(a);
                    if (StringUtils.isNotBlank(x.getString("bk_asst_obj_id"))) {
                        zj = x.getString("bk_property_name");
                    }
                }

                JSONArray result = new JSONArray();
                int i = 0;
                for (int b = 0; b < sl_JSONArray.size(); b++) {
                    //获取到子表里面的数据(实例info)
                    JSONObject y = sl_JSONArray.getJSONObject(b);
                    JSONArray cc = new JSONArray();
                    try {
                        cc = y.getJSONArray(zj);
                    } catch (Exception e) {
                        logger.info("异常原因:{}", e);
                    }
                    if (cc != null && cc.size() > 0) {
                        JSONObject host = new JSONObject();
                        //获取主表里面的数据
                        for (int c = 0; c < cc.size(); c++) {
                            jsonObject.put("bk_obj_id", cc.getJSONObject(c).getString("bk_obj_id"));
                            //查询host数据
                            JSONObject search_inst_url_JSON1 = HttpsClient.httpRequest(apiUrlProperties.getSearch_inst_url(), "POST", jsonObject.toString());
                            JSONArray sl_JSONArray1 = search_inst_url_JSON1.getJSONObject("data").getJSONArray("info");
                            for (int d = 0; d < sl_JSONArray1.size(); d++) {
                                JSONObject useeIndex = new JSONObject();
                                String bk_host_id = sl_JSONArray1.getJSONObject(d).getString("bk_host_id");
                                if (bk_host_id.equals(y.getJSONArray(zj).getJSONObject(c).getString("id"))) {
                                    host.put("host_id", bk_host_id);
                                    JSONObject hostData = sl_JSONArray1.getJSONObject(d);
                                    host.putAll(hostData);
                                    host.putAll(y);
                                    useeIndex.put("useeIndex", i);
                                    host.putAll(useeIndex);
                                    host.put("hostDate", hostData.keySet());
                                    result.add(host);
                                    i++;
                                }
                            }
                        }
                    } else {
                        y.put("useeIndex", i++);
                        result.add(y);
                    }
                }
                return result;
            } catch (Exception e) {
                logger.error("未知异常：{}", e);
            }
        }
        return new JSONArray();
    }

    /**
     * 更新-new
     */
    @RequestMapping("/update")
    @ResponseBody
    public JSONObject update(String updateJsonArray, String bk_supplier_account, String bk_obj_id, HttpServletRequest request) {
        JSONArray jsonArray = JSONArray.parseArray(updateJsonArray);
        if (jsonArray.size() <= 0) {
            JSONObject nullObject = new JSONObject();
            nullObject.put("result", false);
            nullObject.put("code", 1);
            nullObject.put("message", "未获取到编辑信息，请编辑后再次提交");
            nullObject.put("data", "未获取到编辑信息，请编辑后再次提交");
            return nullObject;
        }
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            JSONObject mainObject = commParams.initJsonOtherMethod(request);
            JSONObject fromObject = commParams.initJsonOtherMethod(request);
            JSONObject hostObject = commParams.initJsonOtherMethod(request);
            mainObject.put("bk_supplier_account", bk_supplier_account);

            fromObject.put("bk_supplier_account", bk_supplier_account);
            hostObject.put("bk_supplier_account", bk_supplier_account);
            JSONObject data = new JSONObject();
            Boolean type = false;
            for (String key : jsonObject.keySet()) {
                if ("useeIndex".equals(key)) {
                    continue;
                }
                String[] keys = key.split(",");
                String bk_obj_id_update = keys[0];
                String keyName = keys[1];
                String value = "";
                if (isObject(String.valueOf(jsonObject.get(key)))) {
                    JSONObject object = JSONObject.parseObject(String.valueOf(jsonObject.get(key)));
                    value = object.getString("key");
                    if (StringUtils.isBlank(value)) {
                        value = null;
                    }
                } else {
                    value = String.valueOf(jsonObject.get(key));
                    if ("null".equals(value)) {
                        value = null;
                    }
                }
                if (!"host".equals(bk_obj_id_update) && bk_obj_id_update.equals(bk_obj_id)) {
                    mainObject.put("bk_obj_id", bk_obj_id);
                    mainObject.put(keyName, value);
                } else if ("host".equals(bk_obj_id_update) && bk_obj_id_update.equals(bk_obj_id)) {
                    if ("bk_host_id".equals(keyName)) {
                        hostObject.put(keyName, value);
                    } else {
                        data.put(keyName, value);
                    }
                } else {
                    type = true;
                    fromObject.put("bk_obj_id", bk_obj_id_update);
                    fromObject.put(keyName, value);
                }
            }
            hostObject.put("data", data);
            if (!"host".equals(bk_obj_id)) {
                JSONObject update_inst_url_JSON_main = HttpsClient.httpRequest(apiUrlProperties.getUpdate_inst_URL(), "POST", mainObject.toString());
                if (!update_inst_url_JSON_main.getBoolean("result")) {
                    return update_inst_url_JSON_main;
                }
                if (type) {
                    JSONObject update_inst_url_JSON_from = HttpsClient.httpRequest(apiUrlProperties.getUpdate_inst_URL(), "POST", fromObject.toString());
                    if (!update_inst_url_JSON_from.getBoolean("result")) {
                        return update_inst_url_JSON_from;
                    }
                }
            } else {
                JSONObject update_host_url_JSON_main = HttpsClient.httpRequest(apiUrlProperties.getUpdate_host_URL(), "POST", hostObject.toString());
                if (!update_host_url_JSON_main.getBoolean("result")) {
                    return update_host_url_JSON_main;
                }
                if (type) {
                    JSONObject update_inst_url_JSON_from = HttpsClient.httpRequest(apiUrlProperties.getUpdate_inst_URL(), "POST", fromObject.toString());
                    if (!update_inst_url_JSON_from.getBoolean("result")) {
                        return update_inst_url_JSON_from;
                    }
                }
            }
        }
        JSONObject success = new JSONObject();
        success.put("result", true);
        success.put("code", 1);
        success.put("message", "编辑成功");
        success.put("data", "编辑成功");
        return success;
    }


    /**
     * 更新
     */
    @RequestMapping("/updateDate")
    @ResponseBody
    public JSONObject updateDate(String updateJsonArray, String bk_supplier_account, String bk_obj_id, HttpServletRequest request) {

        System.out.println(updateJsonArray);
//        JSONObject updateJson = commParams.initJson();
//        String bkToken = request.getHeader("Authorization");
        JSONObject updateJson = commParams.initJsonOtherMethod(request);
//        updateJson.put("bk_token",bkToken);
        if (StringUtils.isBlank(bk_supplier_account)) {
            JSONObject errObject = new JSONObject();
            errObject.put("result", false);
            errObject.put("code", 1);
            errObject.put("message", "获取开发商账号失败，请重新尝试！");
            errObject.put("data", "Error");
            return errObject;
        }
        updateJson.put("bk_supplier_account", bk_supplier_account);
        if (StringUtils.isBlank(bk_obj_id)) {
            JSONObject errObject = new JSONObject();
            errObject.put("result", false);
            errObject.put("code", 1);
            errObject.put("message", "获取信息失败，请重新尝试！");
            errObject.put("data", "Error");
            return errObject;
        }
        updateJson.put("bk_obj_id", bk_obj_id);
        //遍历获取每一条的记录信息
        JSONArray jsonArray = JSONArray.parseArray(updateJsonArray);
        JSONArray inst_array = new JSONArray();
        if (StringUtils.isNotBlank(updateJsonArray)) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Long inst_id = StringConverters.ToLong(jsonObject.getString("inst_id"));
                if (StringUtils.isBlank(jsonObject.getString("inst_id"))) {
                    JSONObject errObject = new JSONObject();
                    errObject.put("result", false);
                    errObject.put("code", 1);
                    errObject.put("message", "获取实例信息失败，请重新尝试！");
                    errObject.put("data", "Error");
                    return errObject;
                }
                JSONObject update_data = jsonObject.getJSONObject("update_data");
                //通过列表模块传输的host属性值，判断修改的参数中是否有host属性
                if (update_data != null && update_data.size() > 0) {

                    JSONObject inst_datas = new JSONObject();
                    inst_datas.put("datas", update_data);
                    inst_datas.put("inst_id", inst_id);
                    inst_array.add(inst_datas);
                    updateJson.put("update", inst_array);
                } else {
                    JSONObject errObject = new JSONObject();
                    errObject.put("result", false);
                    errObject.put("code", 1);
                    errObject.put("message", "获取编辑信息失败，请重新尝试！");
                    errObject.put("data", "Error");
                    return errObject;
                }
            }
        } else {
            JSONObject errObject = new JSONObject();
            errObject.put("result", false);
            errObject.put("code", 1);
            errObject.put("message", "获取编辑信息失败，请重新尝试！");
            errObject.put("data", "Error");
            return errObject;
        }
        JSONObject update_inst_url_JSON = HttpsClient.httpRequest(apiUrlProperties.getBatch_update_inst_URL(), "POST", updateJson.toString());
        return update_inst_url_JSON;
    }


    public JSONObject start(String bk_obj_id, HttpServletRequest request) {
        JSONObject map = new JSONObject();
        //JSONObject jsonObject = commParams.initJson();
//        String bkToken = request.getHeader("Authorization");
        JSONObject jsonObject = commParams.initJsonOtherMethod(request);
//        jsonObject.put("bk_token",bkToken);
        jsonObject.put("bk_obj_id", bk_obj_id);
        //查询所有的模型属性
        JSONObject search_object_attribute_URLJSON = HttpsClient.httpRequest(apiUrlProperties.getSearch_object_attribute_URL(), "POST", jsonObject.toString());
        if (search_object_attribute_URLJSON != null) {
            JSONArray search_object_attribute_jsonArray = search_object_attribute_URLJSON.getJSONArray("data");
            if (search_object_attribute_jsonArray != null && search_object_attribute_jsonArray.size() > 0) {
                for (int m = 0; m < search_object_attribute_jsonArray.size(); m++) {
                    JSONObject search_object_attribute_obj = search_object_attribute_jsonArray.getJSONObject(m);
                    map.put(search_object_attribute_obj.getString("bk_property_id"),
                            StringUtils.isBlank(search_object_attribute_obj.getString("bk_property_name")) ? "" : search_object_attribute_obj.getString("bk_property_name"));
                    String bk_asst_obj_id = search_object_attribute_obj.getString("bk_asst_obj_id");
                    if (StringUtils.isNotBlank(bk_asst_obj_id)) {
                        roll(bk_asst_obj_id, map, request);
                    }
                }
            }
        }
        return map;
    }

    public JSONObject roll(String bk_obj_id, JSONObject map, HttpServletRequest request) {
//        JSONObject jsonObject = commParams.initJson();
        String bkToken = request.getHeader("Authorization");
        JSONObject jsonObject = commParams.initJsonOtherMethod(request);
//        jsonObject.put("bk_token",bkToken);
        jsonObject.put("bk_obj_id", bk_obj_id);
        //查询所有的模型属性
        JSONObject search_object_attribute_URLJSON = HttpsClient.httpRequest(apiUrlProperties.getSearch_object_attribute_URL(), "POST", jsonObject.toString());
        if (search_object_attribute_URLJSON != null) {
            JSONArray search_object_attribute_jsonArray = search_object_attribute_URLJSON.getJSONArray("data");
            for (int m = 0; m < search_object_attribute_jsonArray.size(); m++) {
                JSONObject search_object_attribute_obj = search_object_attribute_jsonArray.getJSONObject(m);
                map.put(search_object_attribute_obj.getString("bk_property_id"),
                        StringUtils.isBlank(search_object_attribute_obj.getString("bk_property_name")) ? "" : search_object_attribute_obj.getString("bk_property_name"));
                String bk_asst_obj_id = search_object_attribute_obj.getString("bk_asst_obj_id");
                if (StringUtils.isNotBlank(bk_asst_obj_id)) {
                    roll(bk_asst_obj_id, map, request);
                }
            }
        }
        return map;
    }

    public void getget(String str) {

    }


    public Boolean isObject(String msg) {
        if ("null".equals(msg) || StringUtils.isBlank(msg)) {
            return false;
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(msg);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("bk_app_code", "bk-jingli");
        jsonObject.put("bk_app_secret", "8a2d0412-e9be-4c46-a7a3-c32fb3852bec");
        jsonObject.put("bk_token", "y1qYJTRGHaFAosWYDKqqleAjyn_IF0ro4eXYfaIFkhg");
        jsonObject.put("bk_supplier_account", "0");
        jsonObject.put("bk_obj_id", "qy");
        JSONObject delete = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        jsonArray.add(208);
        delete.put("inst_ids", jsonArray);
        jsonObject.put("delete", delete);
        JSONObject delete_inst_URLJSON = HttpsClient.httpRequest("https://paas.365ops.cn:443/api/c/compapi/v2/cc/batch_delete_inst", "POST", jsonObject.toString());
        System.out.println(delete_inst_URLJSON.getBoolean("result"));
    }

}
